<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" v-if="~accessable.indexOf('manage:message_add')" type="primary" v-waves icon="el-icon-plus" @click="handleUpdate(null)">添加</el-button>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="消息标题" v-model="filters.like$title"></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" type="primary" v-if="~accessable.indexOf('manage:message_export')" v-waves icon="el-icon-document" @click="exportExcelFile">导出</el-button>
    </div>
    <el-table :data="list" @sort-change="sortChange" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="80">
      </el-table-column>

      <el-table-column width="100" prop="users" align="center" label="对象">
        <template slot-scope="scope">
          {{scope.row.users|usersFilter}}
        </template>
      </el-table-column>

      <el-table-column prop="title" align="center" label="标题" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>

      <el-table-column prop="startDate" align="center" label="生效时间" width="170">
        <template slot-scope="scope">
          <span>{{scope.row.startDate}}</span>
        </template>
      </el-table-column>


      <el-table-column prop="endDate" align="center" label="失效时间" width="170">
        <template slot-scope="scope">
          <span>{{scope.row.endDate}}</span>
        </template>
      </el-table-column>


      <el-table-column width="100" prop="status" align="center" label="发布状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statustagFilter">{{scope.row.status|statusFilter}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="createTime" align="center" label="操作时间" width="170">
        <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>

      <el-table-column prop="content" align="center" label="消息内容" width="220">
        <template slot-scope="scope">
          {{scope.row.content}}
        </template>
      </el-table-column>


      <el-table-column align="center" label="操作" width="200" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="primary" v-if="~accessable.indexOf('manage:message_modify')" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" v-if="~accessable.indexOf('manage:message_delete')" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="params.page"
        :page-sizes="[10,20,30,50]" :page-size="params.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="isEditing?'修改':'添加'" :visible.sync="dialogFormVisible">
      <el-form class="dialog-form" :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="120px">

        <el-form-item label="对象" prop="users">
          <el-select v-model="temp.users" placeholder="请选择">
            <el-option v-for="(item,value) in usersFilter" :key="value" :label="item" :value="value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="标题" prop="title">
          <el-input v-model="temp.title" maxlength="50" placeholder="标题"></el-input>
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="生效时间" prop="startDate">
              <el-date-picker v-model="temp.startDate"   format="yyyy 年 MM 月 dd 日"
      value-format="yyyy-MM-dd" align="right" type="datetime" placeholder="选择生效时间" :picker-options="pickerOptions1">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="失效时间" prop="endDate">
              <el-date-picker   format="yyyy 年 MM 月 dd 日"
      value-format="yyyy-MM-dd"  v-model="temp.endDate" align="right" type="datetime" placeholder="选择失效时间" :picker-options="pickerOptions1">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="发布状态" prop="status">

          <el-radio-group v-model="temp.status">
            <el-radio v-for="(item,value) in statusFilter" :key="value" :label="value" :value="value">{{item}}</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="消息内容" prop="content">
          <div class="quill-wrap" style="height:350px;">
            <quill-editor style="height:300px;" v-model="temp.content" ref="myQuillEditor" :options="editorOption">
            </quill-editor>
          </div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="submiting" @click="submitFormAction">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>
  .avatar-uploader {
    width: 375px;
    height: 155px;
  }

  .avatar-uploader>>>.avatar {
    width: 375px;
    height: 155px;
  }

  .avatar-uploader>>>.avatar-uploader-icon {
    width: 375px;
    height: 155px;
  }
  .img-src{
    width:100px;
  }

</style>

<script>
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
// import { container, ImageExtend, QuillWatch } from "quill-image-extend-module";

import { quillEditor, Quill } from "vue-quill-editor";
  let statusFilter = ["上线", "下线"];
  let statustagFilter = ["success", "danger", "info", "warning"];

  let usersFilter = ["全部会员"];
  let userstagFilter = ["success"];

  export default {
    components: {
      quillEditor
    },
    
    data() {
      return {
        total:0,
        temp: {
          id: "",

          users: "",

          title: "",

          startDate: "",

          endDate: "",

          status: "",

          content: ""
        },
          editorOption: {
        placeholder: "在这里输入内容",
        modules: {
          // ImageExtend: {
          //   loading: true,
          //   name: "file",
          //   action: this.UPLOAD_URL,
          //   response: res => {
          //     return res.info;
          //   }
          // }
          // ,
          // toolbar: {
          //   container: container,
          //   handlers: {
          //     image: function() {
          //       QuillWatch.emit(this.quill.id);
          //     }
          //   }
          // }
        }
      },
        pickerOptions1: {
         
          shortcuts: [{
              text: "今天",
              onClick(picker) {
                picker.$emit("pick", new Date());
              }
            },
            {
              text: "昨天",
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() - 3600 * 1000 * 24);
                picker.$emit("pick", date);
              }
            },
            {
              text: "一周前",
              onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", date);
              }
            }
          ]
        },
        statusFilter: statusFilter,

        cityIdOptions: [],

        usersFilter: usersFilter,

        rules: {
          users: [{
            required: true,
            message: "对象不能为空",
            trigger: "change"
          }],

          title: [{
              required: true,
              message: "消息标题不能为空",
              trigger: "change"
            },
            {
              max: 50,
              message: "消息标题不能大于50个字",
              trigger: "change"
            }
          ],

          startDate: [{
            required: true,
            message: "生效时间不能为空",
            trigger: "change"
          }],

          endDate: [{
            required: true,
            message: "失效时间不能为空",
            trigger: "change"
          }],

          status: [{
            required: true,
            message: "请选中发布状态",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.init("message", 1).loadData();

      // this.combOptions("city").then(info => {
      //   this.cityIdOptions = info;
      // });
    },
    filters: {
      statusFilter(status) {
        return statusFilter[parseInt(status)];
      },
      statustagFilter(status) {
        return statustagFilter[parseInt(status)];
      },

      usersFilter(status) {
        return usersFilter[parseInt(status)];
      },
      adPositiontagFilter(status) {
        return adPositiontagFilter[parseInt(status)];
      }
    },
    methods: {
      exportExcelFile() {
        this.exportExcel({
          title: "消息管理",
          fields: [{
              field: "users",
              title: "对象",
              valueMap: ["全部会员"]
            },
            
            {
              field: "title",
              title: "消息标题"
            },

            {
              field: "startDate",
              title: "生效时间"
            },

            {
              field: "endDate",
              title: "失效时间"
            },

            {
              field: "status",
              title: "发布状态",
              valueMap: ["上线", "下线"]
            },

            {
              field: "content",
              title: "消息内容"
            },

            {
              field: "createTime",
              title: "操作时间"
            }
          ]
        }).then(info => {
          this.openWin(info);
        });
      }
    }
  };

</script>
